<template>
	<view class="content">
		<userSelect v-if="showUser" :list="userList" @cancelConfirm="closeUser" @onConfirm="onConfirm"></userSelect>	
		<u-popup
			mode="center"
			:show="showDate" 
			@close="closeDatePop" 
			:round="50"
		>
			<view class="pop date-pop">
				<view class="title">
					请选择截止日期
				</view>
				<view class="body" >
					<uni-calendar 
						:insert="true"
						@change="handleSelectDate">
					</uni-calendar>
				</view>
				<view class="btns-container">
					<view class="custom-button" @click="closeDatePop()">
						取消
					</view>
					<view class="custom-button" @click="confirmSelectDate()">
						确认
					</view>
				</view>
				
			</view>
		</u-popup>
		
		<view class="section-0">
			<view class="msg">
				<view class="title">
					<u--input
						placeholderClass="input-placeholder"
						class="input-body"
					    placeholder="请输入标题"
						:readonly="type !== 'add'"
					    v-model="form.yj_title"
					  ></u--input>
				</view>
				<view class="textarea">
					<u--textarea
						:maxlength="-1"
						placeholderClass="textarea-placeholder"
						class="textarea-body"
					    placeholder="请输待办详情"
					    v-model="form.yj_content"
						border='null'
						:disabled="type !== 'add'"
					  ></u--textarea>
				
				</view>
			</view>
			
			<view class="item" @click="handleShowUser()"> 
				<navigateItem>
					<template slot="left">
						<image mode="aspectFit" class="image-left" src="../../../src/imgs/todo-info-icon-user.png" alt="">
					</template>
					<template slot="content" v-if="type === 'add'">
						<view class="member">
							<text v-if="type === 'add' && form.receive_user === ''">请选择执行人</text>
							<text v-if="form.receive_user !== ''">{{form.receive_user}}</text>
						</view>
					</template>
					<template slot="content" v-if="type === 'receive'">
						<view class="member" >
							<text>发送人：{{form.create_user}}</text>
						</view>
					</template>
					<template slot="content" v-if="type === 'send'">
						<view class="member member__send" >
							<view class="users">{{form.receive_user}}</view>
							<view class="unfinished">
								{{receiveList.filter(item => item.status === "1").length}}人未完成
							</view>
						</view>
					</template>
					<template slot="content" v-if="type === 'done'">
						<view class="member" >
							<text>接收人：{{form.create_user}}</text>
						</view>
					</template>
					<template slot="right" v-if="type !== 'receive' && type !== 'done'">
						<image mode="aspectFit" class="image-right" src="../../../src/imgs/account-icon-right.png" alt="">
					</template>
				</navigateItem>
			</view>
			<view class="item" @click="handleShowDate()">
				<navigateItem>
					<template slot="left">
						<image mode="aspectFit" class="image-left" src="../../../src/imgs/todo-info-icon-time.png" alt="">
					</template>
					<template slot="content" v-if="type === 'add'">
						<text v-if="type === 'add' && form.end_date === ''">请选截止日期</text>
						<text v-if="form.end_date !== ''">{{form.end_date}}</text>
					</template>
					<template slot="content" v-if="type !== 'add'">
						<text>{{form.end_date.substring(0, 10)}}</text>
					</template>
					<template slot="right" v-if="type === 'add'">
						<image mode="aspectFit" class="image-right" src="../../../src/imgs/account-icon-right.png" alt="">
					</template>
				</navigateItem>
			</view>
			<view class="item" @click="handleUpload()">
				<navigateItem>
					<template slot="left">
						<image mode="aspectFit" class="image-left" src="../../../src/imgs/todo-info-icon-file.png" alt="">
					</template>
					<template slot="content">
						<text>附件</text>
					</template>
					<template slot="right" v-if="type === 'add'">
						<image mode="aspectFit" class="image-right" src="../../../src/imgs/account-icon-right.png" alt="">
					</template>
					<template slot="extra">
						<view v-if="file_name !== ''" class="file-name" @click="handleDownload(file_path)">
							{{file_name}}
						</view>
					</template>
				</navigateItem>
			</view>
			<view v-if="type === 'add'" class="custom-button" @click="handleAdd()">
				提交
			</view>
			<view v-if="type === 'receive'" class="custom-button" @click="handleUpdate()">
				完成
			</view>
		</view>
		<l-file 
		    ref="todoInfoFile" 
		    @up-success="uploadSuccess"
		></l-file>
	</view>
</template>

<script>
	import userSelect from '@/components/user_select/index.vue';
	import { mapGetters } from 'vuex';
	import { todoInfoApi, todoAddApi, todoUpdateApi, todoReceiveListApi } from '@/api/todo.js'
	import { uploadApi, fileInfoApi } from '@/api/common.js'
	import navigateItem from '@/components/navigate_item/index.vue'
	export default {
		components: {
			navigateItem, userSelect
		},
		data() {
			return {
				checked: false,
				type: '',
				id: '',
				form: {
					yj_title: '',
					yj_content: '',
					receive_user: '',
					end_date: '',
					file_id: '',
				},
				receiveList: [],
				file_name: '',
				file_path: '',
				selectedDate: '',
				showDate: false,
				showUser: false,
				userList: [],
				selectedUser: [],
			};
		},
		computed: {
			...mapGetters({ 
				userInfo: 'system/getUserInfo',
			}),
		},
		methods: {
			onConfirm(res) {
				this.closeUser();
				this.userList = res;
				let str = res
					.map(item => item.nick_name)
					.join(',');
				this.form.receive_user = str;
			},
			handleShowUser() {
				if (this.type === 'add') {
					this.showUser = true;
				} else if(this.type === 'send') {
					uni.navigateTo({
						url: '/pages/todo/info/summary/index?id=' + this.id
					})
				}
			}, 
			closeUser() {
				this.showUser = false;
			},
			handleShowDate() {
				if (this.type === 'send' || this.type === 'add') {
					this.showDate = true;
				}
			}, 
			closeDatePop() {
				this.showDate = false;
			},
			handleSelectDate(time) {
				this.selectedDate = time.fulldate;
			},
			confirmSelectDate() {
				this.form.end_date = this.selectedDate;
				this.closeDatePop();
			},
			handleUpload() {
				if (this.type !== 'send' && this.type !== 'add' ) {
					return;
				}
				this.$refs.todoInfoFile.upload({
				    // #ifdef APP-PLUS
				    //（app端必传）nvue页面使用时请查阅nvue获取当前webview的api，当前示例为vue窗口
				    currentWebview: this.$mp.page.$getAppWebview(),
				    // #endif
				    //替换为你的上传接口地址
				    url: 'http://58.215.235.4:9016/app/upload',
				    // 服务端接收附件的key
				    name: 'file',
				    //根据你接口需求自定义 (优先不传content-type,安卓端无法收到参数再传)
				    // header: {
				    //     'Authorization': 'bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySW5mbyI6eyJ1c2VyTmFtZSI6',
				    //     'uid': '27682',
				    //     'client': 'app',
				    //     'accountid': 'DPOA9487'
				    // },
				    // 限制选择附件的大小上限，默认10M
				    // maxSize: 20,
				    // 若需要在body单独添加附件名或附件大小如下方式传入组件：
				    // addName: '后端要的附件名称字段key,此处请勿写name的同值如(file)，会覆盖name',
				    // addSize: '后端要的附件大小字段key'
				    // body参数直接写key,value,如：
				    // date: '2020-1-1',
				    // key2: 'value2',
				});
			},
			handleDownload(url) {
				this.$refs.todoInfoFile.download({url})
				// this.$refs.todoInfoFile.download({url})
				// .then(path=>{
				//     this.$refs.todoInfoFile.open(path);
				// });
			},
			uploadSuccess(res) {
				if (res.code === 1) {
					this.form.file_id = res.data.file_id;
					this.file_name = res.data.file_name;
					this.file_path = res.data.file_path;
				}
			},
			async handleAdd() {
				if (this.type === 'add') {
					if (this.form.receive_user === '') {
						uni.showToast({title: '请选择执行人', icon: 'error', duration: 1000});
						return;
					}
					if (this.form.end_date === '') {
						uni.showToast({title: '请选择截止日期', icon: 'error', duration: 1000});
						return;
					}
					await this.addTodo({...this.form, create_user: this.userInfo.nick_name, yj_type: '工作消息'})
					setTimeout(() => {
						this.goBack();
					}, 1000);
				}
			},
			async handleUpdate() {
				await this.updateTodo();
				setTimeout(() => {
					this.goBack();
				}, 1000);
			},
			goBack() {
				uni.navigateBack({
				    delta: 1
				});
			},
			async fetchInfo() {
				uni.showLoading({title: '加载中...'});
				const { data } = await todoInfoApi({id: this.id});
				uni.hideLoading();
				if (data.code === 1) {
					this.form = data.data;
				}
			},
			async addTodo(params) {
				uni.showLoading({title: '加载中...'});
				const { data } = await todoAddApi(params);
				uni.hideLoading();
				if (data.code === 1) {
					this.userList = data.data.list;
					uni.showToast({title: '提交成功', icon: 'success', duration: 1000});
				}
			},
			async updateTodo(params) {
				uni.showLoading({title: '加载中...'});
				const { data } = await todoUpdateApi({link_id: this.id, receive_user: this.userInfo.nick_name});
				uni.hideLoading();
				if (data.code === 1) {
					uni.showToast({title: '提交成功', icon: 'success', duration: 1000});
				}
			},
			async fileInfo() {
				uni.showLoading({title: '加载中...'});
				const { data } = await fileInfoApi({id: this.form.file_id});
				uni.hideLoading();
				if (data.code === 1) {
					this.file_name = data.data.file_name;
					this.file_path = data.data.file_path;
				}
			},
			async todoReceiveList() {
				uni.showLoading({title: '加载中...'});
				const { data } = await todoReceiveListApi({id: this.id});
				uni.hideLoading();
				if (data.code === 1) {
					this.receiveList = data.data.list;
					this.userList = this.receiveList.map(item => ({...item, nick_name: item.receive_user}));
					this.userList.forEach((item, idx) => {
						if (item.status === '2') {
							this.selectedUser.push(idx);
						}
					})
				}
			},
		},
		async onLoad(params) {
			switch (params.type) {
				case 'add':
					uni.setNavigationBarTitle({
						title: '待办(新增)'
					});break;
				case 'receive':
					uni.setNavigationBarTitle({
						title: '待办(接收)'
					});break;
				case 'send':
					uni.setNavigationBarTitle({
						title: '待办(发送)'
					});break;
				case 'done':
					uni.setNavigationBarTitle({
						title: '待办(完成)'
					});break;
				default:
					uni.setNavigationBarTitle({
						title: '待办'
					});break;
			}
			this.type = params.type;
			this.id = params.id;
			if (this.type !== 'add') {
				await this.fetchInfo();
				this.form.file_id && await this.fileInfo();
				if (this.type === 'send') {
					await this.todoReceiveList();
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.actions{
		width: 100%;
		display: flex;
		justify-content: space-between;
		box-sizing: border-box;
		padding: 0px 25px;
		.checkbox{
			font-size: 26rpx;
			font-weight: normal;
			font-stretch: normal;
			letter-spacing: 8rpx;
			color: #333;
			width: 200rpx;
			padding-top: calc((62rpx - 18px) / 2);
		}
	}
	// /deep/{
	// 	.u-index-list__letter__item__index{
	// 		color: #fff!important;
	// 	}
	// }
	.content{
		width: 100%;
		background-color: #fff;
		position: relative;
		.pop{
			width: 671rpx;
			padding-bottom: 40rpx;
			background-color: #fff;
			border-radius: 35rpx;
			overflow: hidden;
			.title{
				width: 100%;
				height: 96rpx;
				line-height: 96rpx;
				background-color: #1534ce;
				font-size: 42rpx;
				font-weight: normal;
				font-stretch: normal;
				letter-spacing: 4rpx;
				color: #ffffff;
				text-align: center;
			}
		}
		.user-pop{
			.body{
				padding: 40rpx 50rpx;
				.wrapper{
					width: 100%;
					height: 540rpx;
					overflow-y: auto;
					overflow-x: hidden;
				}
				.item{
					width: auto;
					height: 60rpx;
					@include clear();
					.check{
						width: 24rpx;
						height: 24rpx;
						border: solid 1rpx #000000;
						border-radius: 50%;
						float: left;
						margin-top: 16rpx;
						position: relative;
						.check-on{
							transition: 0.3s ease;
							opacity: 0;
							transform: scale(0.5);
							border-radius: 50%;
							position: absolute;
							top: 0;
							left: 0;
							right: 0;
							bottom: 0;
							margin: auto;
							width: 18rpx;
							height: 18rpx;
							background-color: #000000;
						}
					}
					.name{
						float: left;
						font-size: 32rpx;
						font-weight: 300;
						height: 60rpx;
						font-stretch: normal;
						line-height: 60rpx;
						letter-spacing: 0rpx;
						color: #000000;
						margin-left: 20rpx;
					}
					.check__active{
						.check-on{
							opacity: 1;
							transform: scale(1);
						}
					}
				}
			}
		}
		.date-pop{
			.body{
				width: 100%;
				box-sizing: border-box;
				padding-bottom: 50rpx;
				/deep/ {
					.uni-calendar-item__weeks-box-item{
						width: 50rpx;
						height: 50rpx;
						font-size: 28rpx;
						font-weight: normal;
						font-stretch: normal;
						line-height: 50rpx;
						letter-spacing: 0rpx;
						color: #000000;
						margin-top: 20rpx;
						margin-bottom: 20rpx;
					}
					.uni-calendar-item--isDay-text{
						color: #000;
					}
					.uni-calendar-item__weeks-lunar-text{
						display: none;
					}
					.uni-calendar-item--checked{
						background: none;
						opacity: 1;
						.uni-calendar-item__weeks-box-item{
							background: #1534ce;
							color: #fff;
							border-radius: 50%;
						}
					}
					.uni-calendar__backtoday{
						display: none;
					}
					.uni-calendar__header-text{
						font-size: 42rpx;
						font-weight: normal;
						font-stretch: normal;
						letter-spacing: 4rpx;
						color: #1534ce;
					}
					.uni-calendar__header{
						justify-content: space-between;
						height: 100rpx;
						line-height: 100rpx;
					}
					.uni-calendar-item--isDay{
						background: none;
						opacity: 1;
						color: #fff!important;
						 .uni-calendar-item__weeks-box-item{
							background: #1534ce;
							color: #fff;
							border-radius: 50%;
						}
					}
				}
			}
			.btns-container{
				width: 100%;
				display: flex;
				justify-content: space-between;
			}
		}
		.section-0{
			width: 100%;
			border-top-left-radius: 50rpx;
			border-top-right-radius: 50rpx;
			background-color: #fff;
			padding-top: 50rpx;
			.custom-button{
				margin-top: 50rpx;
			}
			.file-name{
				width: 100%;
				box-sizing: border-box;
				font-size: 25rpx;
				font-weight: 300;
				font-stretch: normal;
				letter-spacing: 0rpx;
				color: #1534ce;
				padding-left: 35rpx;
				padding-right: 35rpx;
				padding-bottom: 35rpx;
				padding-top: 10rpx;
			}
			.image-left{
				width: 34rpx;
				height: 79rpx;
			}
			.image-right{
				width: 16rpx;
				height: 79rpx;
			}
			.member{
				max-width: 500rpx;
				overflow:hidden;
				text-overflow:ellipsis;
				white-space:nowrap;
			}
			.member__send{
				.users{
					float: left;
					width: 330rpx;
					overflow:hidden;
					text-overflow:ellipsis;
					white-space:nowrap;
				}
				.unfinished{
					display: block;
					width: 170rpx;
					color: #1534ce;
					float: right;
					text-align: right;
				}
			}
			.item{
				margin-bottom: 34rpx;
			}
			.msg{
				width: 672rpx;
				height: 396rpx;
				background-color: #ffffff;
				border-radius: 35rpx;
				margin: 0 auto;
				margin-bottom: 34rpx;
				box-shadow: 0 0 28rpx 14rpx rgba(#bcbcb5, 0.17);
				overflow: hidden;
				.title{
					width: 100%;
					height: 96rpx;
					line-height: 96rpx;
					background-color: #1534ce;
					.input-body{
						box-sizing: border-box;
						width: 100%;
						height: 96rpx;
						padding: 0!important;
						border: none;
						padding-left: 30rpx!important;
						padding-right: 30rpx!important;
						font-size: 42rpx;
						font-weight: normal;
						font-stretch: normal;
						letter-spacing: 0rpx;
						/deep/ .uni-input-input{
							color: #fff;
						}
					}
					
					.input-placeholder{
						font-size: 42rpx;
						font-weight: normal;
						font-stretch: normal;
						letter-spacing: 4rpx;
						color: rgba(255, 255, 255, 0.2);
					}
				}
				.textarea{
					width: 100%;
					height: 300rpx;
					line-height: 30rpx;
			
					.textarea-body{
						box-sizing: border-box;
						width: 100%;
						height: 300rpx;
						border: none;
						padding: 28rpx 30rpx!important;
						font-weight: normal;
						font-stretch: normal;
						letter-spacing: 0rpx;
						/deep/ .u-textarea__field{
							height: auto!important;
							font-size: 25rpx;
						}
					}
					/deep/ .u-textarea--disabled{
						background-color: #fff;
					}
				
					.textarea-placeholder{
						font-size: 25rpx;
						font-weight: normal;
						font-stretch: normal;
						line-height: 50rpx;
						letter-spacing: 3rpx;
						color: rgba(0,0,0,0.5);
					}
				}
			}
		}
	}
</style>
